/*
 * Copyright 2011 Fabian Wenzelmann
 * 
 * This file is part of Recognition-Procedures-for-Boolean-Functions.
 * 
 * Recognition-Procedures-for-Boolean-Functions is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Recognition-Procedures-for-Boolean-Functions is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Recognition-Procedures-for-Boolean-Functions.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.wenzelmf.booleanFunction.util;

import java.io.Serializable;

/**
 * This class is used as a wrapper class for integers but in contrast to the default Integer class it can be used to represent &#8734; and -&#8734;.
 * 
 * There's also a special IntegerValue called {@code NaN} (Not a number). It is for example created if you try to calculate &#8734; - &#8734;. No calculations
 * can be done with NaN.
 * 
 * @author Fabian Wenzelmann
 * @version 1.0
 *
 */
public class IntegerValue implements Comparable<IntegerValue>, Serializable
{

	private static final long serialVersionUID = 703151479698815018L;
	
	/**
	 * Constant for the integer {@code 0}.
	 */
	public static final IntegerValue ZERO = new IntegerValue(0);
	
	/**
	 * Constant for positive infinity &#8734;.
	 */
	public static final IntegerValue POSITIVE_INFINITY = new IntegerValue(true);
	
	/**
	 * Constant for negative infinity -&#8734;.
	 */
	public static final IntegerValue NEGATIVE_INFINITY = new IntegerValue(false);

	/**
	 * This value is set to null if this value neither represents &#8734; nor -&#8734;, {@code true} if it represents &#8734; and {@code false} if it represents -&#8734;. 
	 */
	private final Boolean infinity;
	
	/**
	 * This value is set to null if this value represents &#8734; or -&#8734; and to an integer if this value represents an integer.
	 */
	private final Integer intValue;
	
	/**
	 * The has value is cached for efficiency.
	 */
	private transient Integer cachedHash;
	
	/**
	 * Internal method to calculate the hash code.
	 * 
	 * The has value is just the hash code is just the string representation's hash code. 
	 * 
	 * @return hash code of the string representation
	 */
	private int calcHash()
	{
		return this.toString().hashCode();
	}
	
	/**
	 * Create a new IntegerValue which represents &#8734; if {@code positiveInfinity} is {@code true} and -&#8734; otherwise.
	 * 
	 * @param positiveInfinity If {@code true} this IntegerValue represents &#8734; and -&#8734; otherwise.
	 */
	public IntegerValue(boolean positiveInfinity)
	{
		this.infinity = positiveInfinity;
		this.intValue = null;
	}
	
	/**
	 * Create new IntegerValue representing the integer {@code value}.
	 * 
	 * @param value The integer this IntegerValue represents.
	 */
	public IntegerValue(int value)
	{
		this.infinity = null;
		this.intValue = value;
	}
	
	/**
	 * Create IntegerValue which represents {@code NaN} (Not a Number).
	 */
	public IntegerValue()
	{
		this.infinity = null;
		this.intValue = null;
	}
	
	/**
	 * This method returns {@code true} if this IntegerValue does not represent a number (neither &#8734;, -&#8734; nor an integer).
	 * 
	 * @return {@code true} if this IntegerValue does not represent a number (neither &#8734;, -&#8734; nor an integer).
	 */
	public boolean isNaN()
	{
		return infinity == null && intValue == null;
	}
	
	/**
	 * Returns {@code true} if this IntegerValue represents &#8734;.
	 * 
	 * @return {@code true} if this IntegerValue represents &#8734;.
	 */
	public boolean isPositiveInfinity()
	{
		if(infinity != null)
			return infinity;
		return false;
	}
	
	/**
	 * Returns {@code true} if this IntegerValue represents -&#8734;.
	 * @return {@code true} if this IntegerValue represents -&#8734;.
	 */
	public boolean isNegativeInfinity()
	{
		if(infinity != null)
			return !infinity;
		return false;
	}
	
	/**
	 * Returns the integer value represented by this IntegerValue.
	 * 
	 * @return The integer value represented by this IntegerValue.
	 * 
	 * @throws UnsupportedOperationException If this IntegerValue is {@code NaN}, &#8734; or -&#8734;.
	 */ 
	public int intValue()
	{
		if(isNaN())
		{
			throw new UnsupportedOperationException("The IntegerValue object represents NaN (not a number) and has no int value.");
		}
		if(infinity != null)
		{
			throw new UnsupportedOperationException("The IntegerValue object represents infinity or negative infinity and has no int value.");
		}
		return intValue;
	}
	
	/**
	 * Compare two integer values according to the &lt; relation on integers.
	 * 
	 * Returns -1 if {@code this} &lt; {@code other}, 1 if {@code this} &gt; {@code other} and 0 if {@code this} = {@code other}.
	 * If one of the numbers is {@code NaN} it behaves like the comparison of Doubles.
	 * 
	 * @param other The object to compare with.
	 * @return -1 if {@code this} &lt; {@code other}, 1 if {@code this} &gt; {@code other} and 0 if {@code this} = {@code other}, behaves like Double if one of the values is {@code NaN}.
	 */
	@Override
	public int compareTo(IntegerValue other)
	{
		if(isNaN())
		{
			if(other.isNaN())
			{
				return 0;
			}
			else
			{
				return 1;
			}
		}
		else if(isPositiveInfinity())
		{
			if(other.isNaN())
			{
				return -1;
			}
			else if(other.isPositiveInfinity())
			{
				return 0;
			}
			else
			{
				return 1;
			}
		}
		else if(isNegativeInfinity())
		{
			if(other.isNaN())
			{
				return -1;
			}
			else if(other.isPositiveInfinity())
			{
				return -1;
			}
			else if(other.isNegativeInfinity())
			{
				return 0;
			}
			else
			{
				return -1;
			}
		}
		else
		{
			if(other.isNaN())
			{
				return -1;
			}
			else if(other.isPositiveInfinity())
			{
				return -1;
			}
			else if(other.isNegativeInfinity())
			{
				return 1;
			}
			else
			{
				int thisInt = intValue();
				int otherInt = other.intValue();
				if(thisInt == otherInt)
					return 0;
				if(thisInt < otherInt)
					return -1;
				else
					return 1;
			}
		}
	}
	
	/**
	 * Returns the string representation of this object.
	 * 
	 * The string representation is "NaN", "Infinity", "-Infinity" or the integer as String.
	 * 
	 * @return returns either "NaN", "Infinity", "-Infinity" or the integer as String.
	 */
	@Override
	public String toString()
	{
		if(isNaN())
			return "NaN";
		if(isPositiveInfinity())
			return "Infinity";
		if(isNegativeInfinity())
			return "-Infinity";
		return intValue.toString();
	}
	
	/**
	 * Returns true if other is also an IntegerValue instance and they represent the same number, i.e. returns {@code this.compareTo(other) == 0}.
	 * 
	 * @param other The object to compare with.
	 * 
	 * @return {@code this.compareTo(other) == 0} if other is also an IntegerValue and {@code false} otherwise.
	 */
	@Override
	public boolean equals(Object other)
	{
		if(!(other instanceof IntegerValue))
			return false;
		return this.compareTo((IntegerValue)other) == 0;
	}
	
	/**
	 * Returns the hash code of this IntegerValue, that is the hash code of its string representation.
	 * 
	 * @return The hash code of the string representation of this IntegerValue.
	 */
	@Override
	public int hashCode()
	{
		if(cachedHash == null)
			cachedHash = calcHash();
		return cachedHash;
	}
	
	/**
	 * Calculates {@code this + other}, behaves like addition for Doubles.
	 * 
	 * @param other The IntegerValue that is added to this value, behaves like addition for Doubles.
	 * 
	 * @return The sum of this and other, behaves like addition for Doubles.
	 */
	public IntegerValue add(IntegerValue other)
	{
		if(isPositiveInfinity())
		{
			if(other.isPositiveInfinity())
			{
				return new IntegerValue(true);
			}
			else if(other.isNegativeInfinity())
			{
				return new IntegerValue();
			}
			else if(other.isNaN())
			{
				return new IntegerValue();
			}
			else
			{
				return new IntegerValue(true);
			}
		}
		else if(isNegativeInfinity())
		{
			if(other.isPositiveInfinity())
			{
				return new IntegerValue();
			}
			else if(other.isNegativeInfinity())
			{
				return new IntegerValue(false);
			}
			else if(other.isNaN())
			{
				return new IntegerValue();
			}
			else
			{
				return new IntegerValue(false);
			}
		}
		else if(isNaN())
		{
			return new IntegerValue();
		}
		else
		{
			if(other.isPositiveInfinity())
			{
				return new IntegerValue(true);
			}
			else if(other.isNegativeInfinity())
			{
				return new IntegerValue(false);
			}
			else if(other.isNaN())
			{
				return new IntegerValue();
			}
			else
			{
				return new IntegerValue(intValue() + other.intValue());
			}
		}
	}
	
	/**
	 * Add an {@code int} value to this IntegerValue.
	 * 
	 * This method behaves like {@link #add(IntegerValue)}.
	 * 
	 * @param val The {@code int} value that is added to this IntegerValue.
	 * @return {@code this + val}
	 * @see #add(IntegerValue)
	 */
	public IntegerValue add(int val)
	{
		return add(new IntegerValue(val));
	}
	
	/**
	 * Calculates {@code this * other}, behaves like multiplication for Doubles.
	 * 
	 * @param other The IntegerValue that is multiplied with this value, behaves like multiplication for Doubles.
	 * 
	 * @return The product of this and other, behaves like multiplication for Doubles.
	 */
	public IntegerValue multiply(IntegerValue other)
	{
		if(isPositiveInfinity())
		{
			if(other.isPositiveInfinity())
			{
				return new IntegerValue(true);
			}
			else if(other.isNegativeInfinity())
			{
				return new IntegerValue(false);
			}
			else if(other.isNaN())
			{
				return new IntegerValue();
			}
			else
			{
				int otherVal = other.intValue();
				if(otherVal == 0)
					return new IntegerValue();
				return new IntegerValue( other.intValue() > 0 );
			}
		}
		else if(isNegativeInfinity())
		{
			if(other.isPositiveInfinity())
			{
				return new IntegerValue(false);
			}
			else if(other.isNegativeInfinity())
			{
				return new IntegerValue(true);
			}
			else if(other.isNaN())
			{
				return new IntegerValue();
			}
			else
			{
				int otherVal = other.intValue();
				if(otherVal == 0)
					return new IntegerValue();
				return new IntegerValue( otherVal < 0 );
			}
		}
		else if(isNaN())
		{
			return new IntegerValue();
		}
		else
		{
			if(other.isPositiveInfinity())
			{
				if(intValue == 0)
					return new IntegerValue();
				return new IntegerValue( intValue > 0 );
			}
			else if(other.isNegativeInfinity())
			{
				if(intValue == 0)
					return new IntegerValue();
				return new IntegerValue(intValue < 0);
			}
			else if(other.isNaN())
			{
				return new IntegerValue();
			}
			else
			{
				return new IntegerValue(intValue() * other.intValue());
			}
		}
	}
	
	/**
	 * Calculates {@code this - other}, behaves like subtraction for Doubles.
	 * 
	 * @param other The IntegerValue that is subtracted from this value, behaves like subtraction for Doubles.
	 * 
	 * @return The difference between this and other, behaves like subtraction for Doubles.
	 */
	public IntegerValue subtract(IntegerValue other)
	{
		if(isPositiveInfinity())
		{
			if(other.isPositiveInfinity())
			{
				return new IntegerValue();
			}
			else if(other.isNegativeInfinity())
			{
				return new IntegerValue(true);
			}
			else if(other.isNaN())
			{
				return new IntegerValue();
			}
			else
			{
				return new IntegerValue(true);
			}
		}
		else if(isNegativeInfinity())
		{
			if(other.isPositiveInfinity())
			{
				return new IntegerValue(false);
			}
			else if(other.isNegativeInfinity())
			{
				return new IntegerValue();
			}
			else if(other.isNaN())
			{
				return new IntegerValue();
			}
			else
			{
				return new IntegerValue(false);
			}
		}
		else if(isNaN())
		{
			return new IntegerValue();
		}
		else
		{
			if(other.isPositiveInfinity())
			{
				return new IntegerValue(false);
			}
			else if(other.isNegativeInfinity())
			{
				return new IntegerValue(true);
			}
			else if(other.isNaN())
			{
				return new IntegerValue();
			}
			else
			{
				return new IntegerValue(intValue() - other.intValue());
			}
		}
	}
	
	/**
	 * Subtract an {@code int} value from this IntegerValue.
	 * 
	 * This method behaves like {@link #subtract(IntegerValue)}.
	 * 
	 * @param other The {@code int} value that is subtracted from this IntegerValue.
	 * @return {@code this - val}
	 * @see #subtract(IntegerValue)
	 */
	public IntegerValue subtract(int other)
	{
		return subtract(new IntegerValue(other));
	}
	
	/**
	 * Multiply an {@code int} value with this IntegerValue.
	 * 
	 * This method behaves like {@link #multiply(IntegerValue)}.
	 * 
	 * @param val The {@code int} value that is added to this IntegerValue.
	 * @return {@code this * val}
	 * @see #multiply(IntegerValue)
	 */
	public IntegerValue multiply(int val)
	{
		return multiply(new IntegerValue(val));
	}
	
	/*--------------------------------------- Static methods ---------------------------------------*/
	
	/**
	 * Returns the minimum of v1 an v2.
	 * 
	 * @param v1 The first value.
	 * @param v2 The second value.
	 * @return min{v1, v2}
	 */
	public static IntegerValue min(IntegerValue v1, IntegerValue v2)
	{
		if(v1.compareTo(v2) <= 0)
			return v1;
		return v2;
	}
	
	/**
	 * Returns the maximum of v1 an v2
	 * 
	 * @param v1 The first value.
	 * @param v2 The second value.
	 * @return max{v1, v2}
	 */
	public static IntegerValue max(IntegerValue v1, IntegerValue v2)
	{
		if(v1.compareTo(v2) > 0)
			return v1;
		return v2;
	}
	

	/**
	 * Returns true if this IntegerValue represents an integer.
	 * 
	 * If this value neither presents &#8734; nor -&#8734; nor {@code NaN} returns {@code true} and {@code false} otherwise.
	 * 
	 * @return {@code true} if this value neither presents &#8734; nor -&#8734; nor {@code NaN} and {@code false} otherwise.
	 */
	public boolean isNumber()
	{
		return !(isPositiveInfinity() || isNegativeInfinity() || isNaN());
	}
}
